package com.voxel.sense.infomsg.module.message;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.com.common.view.EmptyView;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.common.VRecyclerFrag;
import com.voxel.sense.infomsg.module.firm.details.FirmInfoDetailActivity;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;
import com.voxel.sense.infomsg.view.titlebar.TextTitleBar;

import butterknife.BindView;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.model.db.MessageModle;
import cyy.example.com.facotry.model.push.BasePushModel;
import cyy.example.com.facotry.model.push.WarningNoticeModel;

/**
 * Created by ChenYiYao on 2018/7/23.
 * 消息模块
 */
public class MessageFrag extends VRecyclerFrag<MessageContract.Presenter, MessageModle> implements MessageContract.View {

    @BindView(R.id.titlebar)
    TextTitleBar titlebar;
    @BindView(R.id.empty_view)
    EmptyView empty_view;
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    private RecyclerAdapter mAdatper;


    @Override
    protected int getContentLayoutId() {
        return R.layout.frag_message;
    }

    @Override
    protected void initData() {
        super.initData();
        titlebar.setTitleText(R.string.message_title);
        titlebar.setLeftVisible(false);
        empty_view.triggerOkOrEmpty(true);
        empty_view.setRefreshBtnVisible(false);
    }


    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        recycle_view.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdatper = new MessageAdatper();
        recycle_view.setAdapter(mAdatper);
        recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_10dp));
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        titlebar.setRightTextAndLis("清除", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.doCleanMsgData();
            }
        });
        mAdatper.setListener(new RecyclerAdapter.AdapterListenerImpl<MessageModle>() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, MessageModle modle) {
                super.onItemClick(holder, modle);
                /**
                 * 判断类型，再进行相应的相应
                 */
                if (BasePushModel.WARNING_MSG.equals(modle.getType())) {
                    WarningNoticeModel data = Factory.getGson().fromJson(modle.getExtra(), WarningNoticeModel.class);
                    FirmInfoDetailActivity.show(getContext(), null,data.getCompanyId());
                }
            }
        });
    }

    @Override
    public RecyclerAdapter<MessageModle> getRecyclerViewAdapter() {
        return mAdatper;
    }

    @Override
    protected MessageContract.Presenter initPresenter() {
        return new MessagePresenter(new MsgRepository(), this);
    }

    @Override
    public void onAdapterDataChanged() {
        super.onAdapterDataChanged();
        empty_view.triggerOkOrEmpty(mAdatper.getItemCount() > 0 ? true : false);
        recycle_view.requestLayout();
    }
}
